package com.echat.config.security.config;

import com.echat.config.security.service.MyUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author chentl
 * @version V1.0
 * @Project BrowserSecurityConfig
 * @Title BrowserSecurityConfig.java
 * @Description 配置类
 * @Package com.echat.config.security.config
 * @date 2019/6/10 11:28
 * @Copyright: 上海顺益信息科技有限公司 All rights reserved.
 */
@Configuration
public class WebSecurityConfig extends GlobalAuthenticationConfigurerAdapter {

    private final MyUserDetailsService userService;


    @Value("${auth.security.browser.loginPage}")
    private String loginPageUrl;

    /**
     * PasswordEncoder 是个接口，该接口下有两个方法，一个是encoder 一个是matches，
     * 前者用于加密，后者用于匹配校验，
     * 我们这里使用的是BCrypt加密算法来实现加密和匹配，
     * 所以在这里实现该接口的encoder方法，进行加密
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


    @Autowired
    public WebSecurityConfig(MyUserDetailsService userService) {
        this.userService = userService;
    }

    @Override
    public void init(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService);
    }

}
